<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>下拉菜单</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" >
    <meta name="generator" content="www.leipi.org" />
    <link rel="stylesheet" href="bootstrap/css/bootstrap.css">
    <!--[if lte IE 6]>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap-ie6.css">
    <![endif]-->
    <!--[if lte IE 7]>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/ie.css">
    <![endif]-->
    <link rel="stylesheet" href="leipi.style.css">
    <link rel="stylesheet" href="../../../../adminlte3.0/plugins/select2/css/select2.css">
    <script type="text/javascript" charset="utf-8" src="../../../js/jquery-1.7.2.min.js?2023"></script>
    <script src="../../../../common/jsite.js"></script>
    <script src="../../../../adminlte3.0/plugins/select2/js/select2.full.js"></script>
    <script src="../../../../adminlte3.0/plugins/select2/js/i18n/zh-CN.js"></script>
    <script src="../../../../modules/icheck-select-init/icheck-select-init.js"></script>
    <script type="text/javascript" src="../dialogs/internal.js"></script>
    <script type="text/javascript">
function createElement(type, name)
{     
    var element = null;     
    try {        
        element = document.createElement('<'+type+' name="'+name+'">');     
    } catch (e) {}   
    if(element==null) {     
        element = document.createElement(type);     
        element.appenname = name;
    } 
    return element;     
}

// Add a new option to a SELECT object (combo or list)
function fnAddComboOption( combo, optionText, optionValue, documentObject, index ) {
    var oOption ;
    if ( documentObject ) {
        oOption = documentObject.createElement("option") ;
    } else {
        oOption = document.createElement("option") ;
    }
    if ( index != null ) {
        combo.options.add( oOption, index ) ;
    } else {
        combo.options.add( oOption ) ;
    }
    oOption.innerHTML = optionText.length > 0 ? fnHTMLEncode( optionText ) : '&nbsp;' ;
    oOption.value     = optionValue ;
    return oOption ;
}
function fnHTMLEncode( text ) {
    if ( !text ) {
        return '' ;
    }
    text = text.replace( /&/g, '&amp;' ) ;
    text = text.replace( /</g, '&lt;' ) ;
    text = text.replace( />/g, '&gt;' ) ;
    return text ;
}

var oNode = null, thePlugins = 'select', selOptions = null;

$(document).ready(function(){
    $("#dictlist").change(function() {
        var dictType = $(this).val();
        js.print(dictType);
        js.ajaxRequest("http://demo.jsite.org.cn/jsite/a/act/flowForm/dictList", {'type':dictType}, function (data) {
            selOptions = data.data;
            var tableData = "";
            for(var i=0;i<data.data.length;i++){
                tableData += "<tr>";
                tableData += "<td>"+data.data[i].type+"</td>"
                tableData += "<td>"+data.data[i].value+"</td>"
                tableData += "<td>"+data.data[i].label+"</td>"
                tableData += "<td>"+data.data[i].description+"</td>"
                tableData+="</tr>"
            }

            $("#tbodyDict").html(tableData)
        }, true);
    });

    js.ajaxRequest("http://demo.jsite.org.cn/jsite/a/act/flowForm/dictList", null, function (data) {
        $("#dictlist").append("<option></option>");
        $.each(data.data, function(index,item){
            $("#dictlist").append("<option value='" + item.type + "'>" + item.description + "</option>");
        });

        if( UE.plugins[thePlugins].editdom ){
            oNode = UE.plugins[thePlugins].editdom;
            var gTitle = oNode.getAttribute('title').replace(/&quot;/g,"\"")
                ,gWidth=oNode.getAttribute('orgwidth')
                ,gType=oNode.getAttribute('orgType');
            gTitle = gTitle==null ? '' : gTitle;
            $G('orgname').value = gTitle;
            $G('orgwidth').value = gWidth;

            js.print(gType);
            $("#dictlist").val(gType).trigger('change');
        }

    }, true);
});

    </script>
</head>
<body>
<div class="content">
    <table class="table table-bordered table-striped table-hover">
     <tr>
        <th><span>控件名称</span><span class="label label-important">*</span></th>
        <th><span>控件样式</span> </th>
    </tr>
    <tr>
        <td><input id="orgname" placeholder="必填项" type="text"/></td>
        <td> 宽：<input id="orgwidth" type="text" value="150" class="input-small span1"/> px&nbsp;&nbsp;&nbsp;&nbsp;高：<input id="orgsize" type="text" class="input-small span1" value="1"/> 行</td>
    </tr>
    <tr>
        <th><span class="pull-right">选择字典</span></th>
        <td> <select id="dictlist" ></select> </td>
    </tr>
    <tr>
        <th colspan="2">
            <span>列表值</span>
        </th>
    </tr>
    <tr>
        <td colspan="2">
            <div style="height: 150px;overflow-y: auto;">
                <table>
                    <tbody id="tbodyDict" >
                    </tbody>
                </table>
            </div>

        </td>
    </tr>
    </table>
</div>
<script type="text/javascript">

dialog.oncancel = function () {
    if( UE.plugins[thePlugins].editdom ) {
        delete UE.plugins[thePlugins].editdom;
    }
};
dialog.onok = function (){
    if( $G('orgname').value == '') {
        js.showWarningMessage('控件名称不能为空');
        return false;
    }
    if( $G('dictlist').value == '' ) {
        js.showWarningMessage('请添加下拉菜单选项！');
        return false;
    }

     var gWidth=$G('orgwidth').value;
     
    if( !oNode ) {
        // try {
            //oNode = document.createElement("select"); 
            oNode = createElement('select',$G('orgname').value);
            oNode.setAttribute('title',$G('orgname').value);
            oNode.setAttribute('name',$G('orgname').value);
            oNode.setAttribute('leipiPlugins',thePlugins );
            // oNode.setAttribute('size',gSize);

            if ( $G('orgwidth').value!= '' ) {
                oNode.style.width =  $G('orgwidth').value+ 'px';
            }
            if( gWidth != '' ) {
                oNode.style.width = gWidth + 'px';
                oNode.setAttribute('orgwidth',gWidth );
            }

            for ( var i = 0 ; i < selOptions.length ; i++ ) {
                var sText = selOptions[i].label;
                var sValue = selOptions[i].value;
                fnAddComboOption( oNode, sText, sValue) ;
            }

            oNode.setAttribute('orgType', $G('dictlist').value);

            //firefox要利用span
            editor.execCommand('insertHtml','<span leipiplugins="select">'+oNode.outerHTML+'&nbsp;&nbsp;</span>');
            return true ;
        // } catch ( e ) {
        //     try {
        //         editor.execCommand('error');
        //     } catch ( e ) {
        //         alert('控件异常，请到 [雷劈网] 反馈或寻求帮助！');
        //     }
        //     return false;
        // }
    } else {
        oNode.setAttribute('title', $G('orgname').value);
        oNode.setAttribute('name', $G('orgname').value);
        if( gWidth != '' ) {
			oNode.style.width = gWidth + 'px';
			oNode.setAttribute('orgwidth',gWidth );
		}


        oNode.options.length=0;
        for ( var i = 0 ; i < selOptions.length ; i++ ) {
            var sText = selOptions[i].label;
            var sValue = selOptions[i].value;
            fnAddComboOption( oNode, sText, sValue) ;
        }

        oNode.setAttribute('orgType', $G('dictlist').value);

        delete UE.plugins[thePlugins].editdom;
    }
};


</script>
</body>
</html>